// Get the y offset of a given node in a web page, accounting for (non-)static
// positioning, scroll positions, table and body node special behaviour and the
// in-page position of surrounding iframe elements, when singleFrame === false.
//
// (Code adapted 2008-03 from Firebug by Joe Hewitt, under the MIT license.)
function getYOffset(node, singleFrame) {
  function recurse(node, win) {
    var parent = node.offsetParent;
    y += node.offsetTop - (parent ? parent.scrollTop : 0);

    if (parent) {
      if (parent.nodeType === 1) {
        var parentStyle = win.getComputedStyle(parent, '');
        if (parentStyle.position !== 'static') {
          y += parseInt(parentStyle.borderTopWidth);
          switch (parent.localName) {
            case 'TABLE':
              y += parseInt(parentStyle.paddingTop); break;
            case 'BODY':
              y += parseInt(win.getComputedStyle(node, '').marginTop); break;
            default:
          }
        }
        else if (parent.localName === 'BODY') {
          y += parseInt(parentStyle.borderTopWidth);
        }

        for (var p = node.parentNode; p !== parent; p = p.parentNode) {
          y -= p.scrollTop;
        }
        recurse(parent, win);
      }
    }
    else {
      if (node.localName === 'BODY') {
        y += parseInt(win.getComputedStyle(node, '').borderTopWidth);
        y -= parseInt(win.getComputedStyle(node.parentNode, '').paddingTop);
      }

      if (node.scrollTop)
        y += node.scrollTop;

      if (win && (!singleFrame && (node = win.frameElement)))
        recurse(node, node.ownerDocument.defaultView);
    }
  }

  var y = 0;
  if (node) recurse(node, node.ownerDocument.defaultView);
  return y;
}
